﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


namespace QuangMobile.Admin
{
    public partial class XemTruocBaiViet : System.Web.UI.Page
    {
        private PrepareDataForTreeView DataForTreeView = new PrepareDataForTreeView();
        private int BaiVietID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] != null)
            {
                BaiVietID = Convert.ToInt32(Request.QueryString["ID"]);
                LoadTinTuc(BaiVietID);
            }
            if (!IsPostBack)
            {
                DataForTreeView.PopulateRootLevel(trvMenu);
            }
        }

        protected void trvMenu_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            DataForTreeView.PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
        }

        protected void trvMenu_SelectedNodeChanged(object sender, EventArgs e)
        {
            Response.Redirect("DanhSachBaiViet.aspx?cID=" + trvMenu.SelectedValue);
        }

        protected void LoadTinTuc(int ID)
        {
            string sql = "SELECT * FROM dbo.tblTinTuc WHERE  ID = " + ID;
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            TieuDe.InnerHtml = dt.Rows[0]["TieuDe"].ToString();
            ImageCaption.InnerHtml = dt.Rows[0]["ImageCaption"].ToString();
            TomTat.InnerHtml = dt.Rows[0]["TomTat"].ToString();
            NoiDung.InnerHtml = dt.Rows[0]["ChiTiet"].ToString();
            Image.ImageUrl = "../" + dt.Rows[0]["Image"].ToString();
        }

        private void PheDuyetBaiViet(bool PheDuyet, int sID)
        {
            string sql = "UPDATE dbo.tblTinTuc SET IsPheDuyet = @IsPheDuyet, PheDuyetDate = GETDATE() WHERE ID = @sID";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@IsPheDuyet", SqlDbType.Bit, ParameterDirection.Input, PheDuyet));
            arrParams.Add(SqlHelper.CreateParameterObject("@sID", SqlDbType.Int, ParameterDirection.Input, sID));
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        protected void btnPheDuyet_Click(object sender, EventArgs e)
        {
            PheDuyetBaiViet(true, BaiVietID);
        }
    }
}
